#include "intprocess.h"


void TIM3_PWM_IntProcess(TIM_HandleTypeDef *htim)
{
	YSO.pulseCount--;
	if(YSO.currentPulsePos < YSO.taragetPulsePos)
	{
		YSO.currentPulsePos += YSO.setPulseLevel;
	}
	else
	{
		YSO.currentPulsePos -= YSO.setPulseLevel;
	}
	if(YSO.pulseCount <= 0)
	{
		YSO.stepStatus = FREE;     //电机空闲状态
		HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);
		HAL_TIM_PWM_Stop_IT(&htim3,TIM_CHANNEL_1);
	}
}
void TIM4_PWM_IntProcess(TIM_HandleTypeDef *htim)
{
	XSO.pulseCount--;
	if(XSO.currentPulsePos < XSO.taragetPulsePos)
	{
		XSO.currentPulsePos += XSO.setPulseLevel;
	}
	else
	{
		XSO.currentPulsePos -= XSO.setPulseLevel;
	}
	if(XSO.pulseCount <= 0)
	{
		XSO.stepStatus = FREE;     //电机空闲状态
		HAL_TIM_PWM_Stop(&htim4,TIM_CHANNEL_1);
		HAL_TIM_PWM_Stop_IT(&htim4,TIM_CHANNEL_1);
	}

}

void TIM2_1ms_IntProcess(void)
{
	 trackFinish();
	//暂停
    if(CO.pauseFlag == 1)
    {
    	return;
    }
    if(CO.receiveFlag == 1)
    {
    	CO.receiveFlag = 0;
    	CO.receiveCount = 0;
    }
    else
    {
    	CO.receiveCount++;
    }
    CO.traceDelayCount++;
    if(CO.traceDelayCount <= 30)
    {
    	return;
    }
    else
    {
    	CO.traceDelayCount =0;
	//状态判断

		XSO.taragetPulsePos = g_red_coordinate.xCoordinate;
		YSO.taragetPulsePos = g_red_coordinate.yCoordinate;
		XSO.currentPulsePos = g_green_coordinate.xCoordinate;
		YSO.currentPulsePos = g_green_coordinate.yCoordinate;

		XSO.distanceLength = XSO.taragetPulsePos-XSO.currentPulsePos;
		YSO.distanceLength = YSO.taragetPulsePos-YSO.currentPulsePos;

		if(absint(XSO.distanceLength) <= 15 && absint(YSO.distanceLength) <= 15)
		{
			CO.traceStartFlag = 0;
			CO.traceFinishFlag = 1;
		}
		else
		{
			CO.workStatus = BUSY;
			CO.traceStartFlag = 1;
			CO.traceFinishFlag = 0;
		}
		YSO_Start();
		XSO_Start();

    }
}

void dividXStep(void)
{
	if(absint(XSO.distanceLength) >= 200)
	{
		XSO.setPulseLevel = 30;
	}
	else if(absint(XSO.distanceLength) >= 150)
	{
		XSO.setPulseLevel = 10;
	}
	else if(absint(XSO.distanceLength) >= 100)
	{
		XSO.setPulseLevel = 10;
	}
	else if(absint(XSO.distanceLength) >= 70)
	{
		XSO.setPulseLevel = 8;
	}
	else if(absint(XSO.distanceLength) >= 50)
	{
		XSO.setPulseLevel = 4;
	}
	else if(absint(XSO.distanceLength) >= 40)
	{
		XSO.setPulseLevel = 3;
	}
	else if(absint(XSO.distanceLength) >= 20)
	{
		XSO.setPulseLevel = 2;
	}	else
	{
		XSO.setPulseLevel = 0;
	}
}

void dividYStep(void)
{
	if(absint(YSO.distanceLength) >= 200)
	{
		YSO.setPulseLevel = 30;
	}
	else if(absint(YSO.distanceLength) >= 150)
	{
		YSO.setPulseLevel = 12;
	}
	else if(absint(YSO.distanceLength) >= 110)
	{
		YSO.setPulseLevel = 10;
	}
	else if(absint(YSO.distanceLength) >= 70)
	{
		YSO.setPulseLevel = 8;
	}
	else if(absint(YSO.distanceLength) >= 50)
	{
		YSO.setPulseLevel = 5;
	}
	else if(absint(YSO.distanceLength) >= 40)
	{
		YSO.setPulseLevel = 3;
	}
	else if(absint(YSO.distanceLength) >= 20)
	{
		YSO.setPulseLevel = 2;
	}
	else
	{
		YSO.setPulseLevel = 0;
	}

}

void raspiDataProcess(RxProBufDef theBuf)

{
  uint8_t mood=0;
  mood=judgmenMood(theBuf );

	uint8_t* tempheader = &theBuf.rxProBuf[6];
  if(mood!=0){

	    if(mood==REDCOORDINATE)
	    {
	      //接收到红色坐标
	   	  //HAL_UART_Transmit(&huart2, "red", 3,100 );
		  g_red_coordinate=singleLightCoordinateGet(tempheader);


		  positionCorrection(set);
//		  g_red_coordinate.xCoordinate=(g_red_coordinate.xCoordinate-g_origin_coordinate.xCoordinate)/(float)g_topright_coordinate.xCoordinate;
//		  g_red_coordinate.yCoordinate=(g_red_coordinate.yCoordinate-g_origin_coordinate.yCoordinate)/(float)g_topright_coordinate.yCoordinate;

		   mood = 0;


	    }

	   if(mood==GREENCOORDINATE) //接收到绿色坐标
	   {
		   	  //HAL_UART_Transmit(&huart2, "green",5,100 );

		   g_green_coordinate=singleLightCoordinateGet(tempheader);
		   mood = 0;
	   }

	   if(mood==RECTANGLE) //接收到矩形框坐标
	   {
		   //HAL_UART_Transmit(&huart2, "rec", 3,100 );

		   rectangleCoordinateGet(tempheader,g_rectangle_coordinate); //矩形坐标
		   mood = 0;

	   }

	   if(mood==REDANDGREENCOORDINATE) //接收到双色激光坐标
	   {
		   doubleLightCoordinateGet(tempheader);
		   mood = 0;

		   dividYStep();
		   dividXStep();
		   YSO.pulseCount = YSO.setPulseLevel;
		   XSO.pulseCount = XSO.setPulseLevel;

	   }

	   if(mood==RASPIMOOD) //接收到树莓派当前工作模式
	   {
		   g_raspi_mood=getRaspiMood(tempheader);
		   mood = 0;

	   }
  }
}
